package com.baizhi.object;

import java.util.Arrays;
/**
 *  如果一个接口没有抽象方法、
 *  表示当前接口仅仅是一个标记性的接口
 *  如果Clonable接口一旦被实现，那么这个实现类的对象允许被复制，否则不允许被复制
 */
public class Teacher implements Cloneable{
    private int id;
    private String name;
    private int age;
    private String[] love;

    public Teacher() {
    }

    public Teacher(int id, String name, int age, String[] love) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.love = love;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String[] getLove() {
        return love;
    }

    public void setLove(String[] love) {
        this.love = love;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", love=" + Arrays.toString(love) +
                '}';
    }

    //浅克隆
//    @Override
//    public Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }

    //深克隆
    @Override
    public Object clone() throws CloneNotSupportedException {
        //先把可供对象中的数组获取到
        String[] data = this.love;
        //创建新的数组
        String[] newData = new String[data.length];
        //拷贝数组中的数据到新数组
        for (int i = 0; i < data.length; i++) {
            newData[i] = data[i];
        }

        //调用父类的克隆方法进行克隆
        //浅克隆
        Teacher t = (Teacher)super.clone();
        //因为父类中的克隆是浅克隆，将父类中引用类型的地址替换成新对象的地址就是深克隆
        t.love = newData;
        return t;
    }
}
